﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace anim3
{


    // UI 管理器，负责管理所有 UI 界面
    public class UIManager : MonoBehaviour
    {
        private static UIManager instance;
        public static UIManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = FindObjectOfType<UIManager>();
                }
                return instance;
            }
        }

        public Dictionary<string, UIBase> uiDictionary = new Dictionary<string, UIBase>();

        private void Awake()
        {
            // 初始化所有 UI 界面
            UIBase[] allUIs = FindObjectsOfType<UIBase>();
            foreach (UIBase ui in allUIs)
            {
                uiDictionary[ui.name] = ui;
                ui.gameObject.SetActive(false);
            }
        }
        private string closingUI = null;
        // 打开指定名称的 UI 界面
        public void OpenUI(string uiName)
        {
            if (closingUI != null)
            {
                // 如果有界面正在关闭，等待关闭完成后再打开新界面
                uiDictionary[closingUI].Close(() =>
                {
                    closingUI = null;
                    if (uiDictionary.ContainsKey(uiName))
                    {
                        uiDictionary[uiName].Open();
                    }
                });
            }
            else
            {
                if (uiDictionary.ContainsKey(uiName))
                {
                    uiDictionary[uiName].Open();
                }
            }
        }

        // 关闭指定名称的 UI 界面
        public void CloseUI(string uiName)
        {
            if (uiDictionary.ContainsKey(uiName))
            {
                closingUI = uiName;
                uiDictionary[uiName].Close(() =>
                {
                    closingUI = null;
                });
            }
        }
    }
}